{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c899a25c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from collections import Counter\n",
    "from sklearn.model_selection import train_test_split\n",
    "from imblearn.over_sampling import SMOTE\n",
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f96778db",
   "metadata": {},
   "source": [
    "### 1.采样\n",
    "- 确保数据均衡"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7cf69abf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>申请号</th>\n",
       "      <th>Y值</th>\n",
       "      <th>X1</th>\n",
       "      <th>X2</th>\n",
       "      <th>X3</th>\n",
       "      <th>X4</th>\n",
       "      <th>X5</th>\n",
       "      <th>X6（对比指标）</th>\n",
       "      <th>X7（对比指标）</th>\n",
       "      <th>X8（对比指标）</th>\n",
       "      <th>...</th>\n",
       "      <th>X13（对比指标）</th>\n",
       "      <th>X14</th>\n",
       "      <th>X15</th>\n",
       "      <th>X16</th>\n",
       "      <th>X17</th>\n",
       "      <th>X18（仅分析中国专利指标）</th>\n",
       "      <th>X19</th>\n",
       "      <th>X20</th>\n",
       "      <th>X21</th>\n",
       "      <th>X22（仅分析中国专利指标）</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>US2007982672A</td>\n",
       "      <td>有效，未转移（Y=0）</td>\n",
       "      <td>35</td>\n",
       "      <td>9</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>102</td>\n",
       "      <td>28</td>\n",
       "      <td>0.009321</td>\n",
       "      <td>...</td>\n",
       "      <td>0.009312</td>\n",
       "      <td>20</td>\n",
       "      <td>120.000000</td>\n",
       "      <td>15.583333</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>122</td>\n",
       "      <td>4.416667</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>US2006499861A</td>\n",
       "      <td>已转移（Y=1）</td>\n",
       "      <td>13</td>\n",
       "      <td>7</td>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>8</td>\n",
       "      <td>0.013783</td>\n",
       "      <td>...</td>\n",
       "      <td>0.013749</td>\n",
       "      <td>24</td>\n",
       "      <td>78.333333</td>\n",
       "      <td>7.833333</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>12.166667</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>US2009422747A</td>\n",
       "      <td>有效，未转移（Y=0）</td>\n",
       "      <td>47</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "      <td>7</td>\n",
       "      <td>0.000668</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000658</td>\n",
       "      <td>35</td>\n",
       "      <td>96.666667</td>\n",
       "      <td>14.166667</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>122</td>\n",
       "      <td>5.833333</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>US2006601842A</td>\n",
       "      <td>已转移（Y=1）</td>\n",
       "      <td>25</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>28</td>\n",
       "      <td>94</td>\n",
       "      <td>0.006946</td>\n",
       "      <td>...</td>\n",
       "      <td>0.006904</td>\n",
       "      <td>136</td>\n",
       "      <td>39.000000</td>\n",
       "      <td>6.583333</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>27</td>\n",
       "      <td>118</td>\n",
       "      <td>13.416667</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>US13507739A</td>\n",
       "      <td>已转移（Y=1）</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>39</td>\n",
       "      <td>13</td>\n",
       "      <td>0.002027</td>\n",
       "      <td>...</td>\n",
       "      <td>0.002012</td>\n",
       "      <td>40</td>\n",
       "      <td>39.000000</td>\n",
       "      <td>6.916667</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>13.083333</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             申请号           Y值  X1  X2  X3  X4  X5  X6（对比指标）  X7（对比指标）  \\\n",
       "0  US2007982672A  有效，未转移（Y=0）  35   9   8   1   0       102        28   \n",
       "1  US2006499861A     已转移（Y=1）  13   7  23   1   0        40         8   \n",
       "2  US2009422747A  有效，未转移（Y=0）  47   5   1   3   1        11         7   \n",
       "3  US2006601842A     已转移（Y=1）  25   7   3   4   2        28        94   \n",
       "4    US13507739A     已转移（Y=1）   0   9   0   4   1        39        13   \n",
       "\n",
       "   X8（对比指标）  ...  X13（对比指标）  X14         X15        X16  X17  X18（仅分析中国专利指标）  \\\n",
       "0  0.009321  ...   0.009312   20  120.000000  15.583333    0               0   \n",
       "1  0.013783  ...   0.013749   24   78.333333   7.833333    0               0   \n",
       "2  0.000668  ...   0.000658   35   96.666667  14.166667    0               0   \n",
       "3  0.006946  ...   0.006904  136   39.000000   6.583333    1               0   \n",
       "4  0.002027  ...   0.002012   40   39.000000   6.916667    0               0   \n",
       "\n",
       "   X19  X20        X21  X22（仅分析中国专利指标）  \n",
       "0    9  122   4.416667               0  \n",
       "1    1    1  12.166667               0  \n",
       "2    4  122   5.833333               0  \n",
       "3   27  118  13.416667               0  \n",
       "4    1    1  13.083333               0  \n",
       "\n",
       "[5 rows x 24 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_excel(r'20240220 样本集.xlsx')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2c60fde5",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['标签'] = df['Y值'].apply(lambda x: 1 if '1' in x else 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0932bfcc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({0: 2103, 1: 504})"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Counter(df['标签'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "554810b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = df.iloc[:,2:-1]\n",
    "y = df['标签']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9cb41d72",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 划分训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "52f948a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({0: 1471, 1: 353})"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对训练集过采样1471+1471\n",
    "Counter(y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d5e1a7b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({0: 632, 1: 151})"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Counter(y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "86dd3991",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据标准化\n",
    "scaler = StandardScaler()\n",
    "X_train = scaler.fit_transform(X_train)\n",
    "X_test = scaler.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "98bc150e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 应用SMOTE算法\n",
    "smote = SMOTE(random_state=42)\n",
    "X_train_smote, y_train_smote = smote.fit_resample(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4469f443",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将过采样后的数据转换回数据框\n",
    "X_train_smote = pd.DataFrame(X_train_smote, columns=X.columns)\n",
    "df_train_smote = X_train_smote\n",
    "df_train_smote['target'] = y_train_smote"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "95534e17",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2942, 23)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>X1</th>\n",
       "      <th>X2</th>\n",
       "      <th>X3</th>\n",
       "      <th>X4</th>\n",
       "      <th>X5</th>\n",
       "      <th>X6（对比指标）</th>\n",
       "      <th>X7（对比指标）</th>\n",
       "      <th>X8（对比指标）</th>\n",
       "      <th>X9（对比指标）</th>\n",
       "      <th>X10（对比指标）</th>\n",
       "      <th>...</th>\n",
       "      <th>X14</th>\n",
       "      <th>X15</th>\n",
       "      <th>X16</th>\n",
       "      <th>X17</th>\n",
       "      <th>X18（仅分析中国专利指标）</th>\n",
       "      <th>X19</th>\n",
       "      <th>X20</th>\n",
       "      <th>X21</th>\n",
       "      <th>X22（仅分析中国专利指标）</th>\n",
       "      <th>target</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.176973</td>\n",
       "      <td>-0.414352</td>\n",
       "      <td>-0.315186</td>\n",
       "      <td>0.346217</td>\n",
       "      <td>-0.570438</td>\n",
       "      <td>-0.325930</td>\n",
       "      <td>-0.089354</td>\n",
       "      <td>-0.216582</td>\n",
       "      <td>-0.323837</td>\n",
       "      <td>-0.310106</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.722584</td>\n",
       "      <td>0.979306</td>\n",
       "      <td>1.141207</td>\n",
       "      <td>-0.141895</td>\n",
       "      <td>1.292904</td>\n",
       "      <td>-0.144716</td>\n",
       "      <td>-0.404947</td>\n",
       "      <td>-0.485694</td>\n",
       "      <td>-0.101543</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.081335</td>\n",
       "      <td>-0.414352</td>\n",
       "      <td>-0.315186</td>\n",
       "      <td>-0.640501</td>\n",
       "      <td>-0.570438</td>\n",
       "      <td>-0.325930</td>\n",
       "      <td>-0.089354</td>\n",
       "      <td>-0.216582</td>\n",
       "      <td>0.853566</td>\n",
       "      <td>-0.310106</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.045518</td>\n",
       "      <td>-0.503059</td>\n",
       "      <td>-0.711890</td>\n",
       "      <td>-0.141895</td>\n",
       "      <td>-0.556419</td>\n",
       "      <td>-0.288801</td>\n",
       "      <td>-0.404947</td>\n",
       "      <td>-0.485694</td>\n",
       "      <td>-0.101543</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.090870</td>\n",
       "      <td>-0.414352</td>\n",
       "      <td>-0.315186</td>\n",
       "      <td>-0.640501</td>\n",
       "      <td>-0.570438</td>\n",
       "      <td>-0.325930</td>\n",
       "      <td>-0.329562</td>\n",
       "      <td>-0.216582</td>\n",
       "      <td>-0.355666</td>\n",
       "      <td>-0.310106</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.807218</td>\n",
       "      <td>-0.279689</td>\n",
       "      <td>-0.743298</td>\n",
       "      <td>-0.141895</td>\n",
       "      <td>-0.556419</td>\n",
       "      <td>-0.288801</td>\n",
       "      <td>-0.404947</td>\n",
       "      <td>-0.485694</td>\n",
       "      <td>-0.101543</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.176973</td>\n",
       "      <td>-0.414352</td>\n",
       "      <td>-0.315186</td>\n",
       "      <td>-0.640501</td>\n",
       "      <td>-0.570438</td>\n",
       "      <td>-0.325930</td>\n",
       "      <td>-0.569770</td>\n",
       "      <td>-0.216582</td>\n",
       "      <td>-0.425902</td>\n",
       "      <td>-0.310106</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.045518</td>\n",
       "      <td>0.139976</td>\n",
       "      <td>-1.245833</td>\n",
       "      <td>-0.141895</td>\n",
       "      <td>-0.556419</td>\n",
       "      <td>-0.288801</td>\n",
       "      <td>-0.404947</td>\n",
       "      <td>-0.485694</td>\n",
       "      <td>-0.101543</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.253540</td>\n",
       "      <td>5.639940</td>\n",
       "      <td>0.276274</td>\n",
       "      <td>-0.640501</td>\n",
       "      <td>-0.570438</td>\n",
       "      <td>1.021117</td>\n",
       "      <td>0.030750</td>\n",
       "      <td>0.430192</td>\n",
       "      <td>-0.134022</td>\n",
       "      <td>-0.310105</td>\n",
       "      <td>...</td>\n",
       "      <td>1.647147</td>\n",
       "      <td>-0.536903</td>\n",
       "      <td>0.701489</td>\n",
       "      <td>-0.141895</td>\n",
       "      <td>-0.556419</td>\n",
       "      <td>-0.144716</td>\n",
       "      <td>-0.404947</td>\n",
       "      <td>1.736652</td>\n",
       "      <td>-0.101543</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         X1        X2        X3        X4        X5  X6（对比指标）  X7（对比指标）  \\\n",
       "0 -0.176973 -0.414352 -0.315186  0.346217 -0.570438 -0.325930 -0.089354   \n",
       "1  0.081335 -0.414352 -0.315186 -0.640501 -0.570438 -0.325930 -0.089354   \n",
       "2 -0.090870 -0.414352 -0.315186 -0.640501 -0.570438 -0.325930 -0.329562   \n",
       "3 -0.176973 -0.414352 -0.315186 -0.640501 -0.570438 -0.325930 -0.569770   \n",
       "4  0.253540  5.639940  0.276274 -0.640501 -0.570438  1.021117  0.030750   \n",
       "\n",
       "   X8（对比指标）  X9（对比指标）  X10（对比指标）  ...       X14       X15       X16       X17  \\\n",
       "0 -0.216582 -0.323837  -0.310106  ... -0.722584  0.979306  1.141207 -0.141895   \n",
       "1 -0.216582  0.853566  -0.310106  ... -0.045518 -0.503059 -0.711890 -0.141895   \n",
       "2 -0.216582 -0.355666  -0.310106  ... -0.807218 -0.279689 -0.743298 -0.141895   \n",
       "3 -0.216582 -0.425902  -0.310106  ... -0.045518  0.139976 -1.245833 -0.141895   \n",
       "4  0.430192 -0.134022  -0.310105  ...  1.647147 -0.536903  0.701489 -0.141895   \n",
       "\n",
       "   X18（仅分析中国专利指标）       X19       X20       X21  X22（仅分析中国专利指标）  target  \n",
       "0        1.292904 -0.144716 -0.404947 -0.485694       -0.101543       0  \n",
       "1       -0.556419 -0.288801 -0.404947 -0.485694       -0.101543       0  \n",
       "2       -0.556419 -0.288801 -0.404947 -0.485694       -0.101543       0  \n",
       "3       -0.556419 -0.288801 -0.404947 -0.485694       -0.101543       0  \n",
       "4       -0.556419 -0.144716 -0.404947  1.736652       -0.101543       1  \n",
       "\n",
       "[5 rows x 23 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1471+1471\n",
    "print(df_train_smote.shape)\n",
    "df_train_smote.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "fe80023b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({0: 1471, 1: 1471})"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Counter(df_train_smote['target'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ed0b77de",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 训练集打乱顺序\n",
    "df_train_smote = df_train_smote.sample(frac=1).reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a6058bba",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试集打乱顺序\n",
    "df_test = pd.DataFrame(X_test,columns=X.columns)\n",
    "df_test['target'] = y_test.values\n",
    "df_test = df_test.sample(frac=1).reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0e80a6f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({0: 632, 1: 151})"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Counter(df_test['target'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "249464f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# df_train_smote.to_excel(r'train.xlsx',index = False)\n",
    "# df_test.to_excel(r'test.xlsx',index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35f9c388",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32a0b55d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9310009b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85131e75",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
